package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Driver;
import com.example.service.IDriverService;
import com.example.util.Code;
import com.example.util.Result;
import com.example.util.page;
import com.example.util.page2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2023-11-03
 */
@RestController
@RequestMapping("/s/driver")
public class DriverController {

    @Autowired
    private IDriverService driverService;


    /**
     * 司机登录
     * @param driver
     * @param result
     * @return
     */
    @PostMapping("/zhuce")
    public Result login(@RequestBody @Valid Driver driver, BindingResult result) {
        String str = "";
        if (result.hasErrors()) {
            for (ObjectError item : result.getAllErrors()) {
                str = item.getDefaultMessage();
            }
            return new Result(Code.LOGIN_ERR, "登陆失败", str);

        } else {
            String phoneNumber = driver.getPhone();
            String regex = "^1[3456789]\\d{9}$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(phoneNumber);
            if (matcher.matches()) {
                String phone = driver.getPhone();
                Driver driver1 = driverService.selectByPhone(phone);
                if(driver1 != null){
                    return new Result(Code.LOGIN_OK,driver1,"已经注册过");
                }else  {
                    int insert = driverService.insert(driver);
                    if(insert>0){
                        return new Result(Code.LOGIN_OK,insert,"注册成功");
                    }else{
                        return new Result(Code.LOGIN_ERR,"注册成功");
                    }
                }

            }

        }
        return new Result(Code.LOGIN_ERR,"登陆失败");
    }
//    TTT查询油气积分
    @GetMapping("/findByDid/{id}")
    public Result findByDid(@PathVariable Integer id){
        Map byDid = driverService.findByDid(id);
        if(byDid!=null){
            return new Result(Code.GET_OK,byDid,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
//    TTT查询总积分
    @GetMapping("/findDAllPoints/{id}")
    public Result findDAllPoints (@PathVariable Integer id){
        Map dAllPoints = driverService.findDAllPoints(id);
        if(dAllPoints!=null){
            return new Result(Code.GET_OK,dAllPoints,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }

    }

//    TTT验证密码
    @PostMapping("/SelectPassword/{passwd}")
    public Result SelectPassword(@PathVariable String passwd){
        Driver driver = driverService.SelectPassword(passwd);
        if (driver!=null) {
            return new Result(Code.GET_OK,"密码正确");
        }else {
            return new Result(Code.GET_ERR,"密码错误");
        }
    }





    @PutMapping("/update")
    public Result update(@RequestBody Driver driver) {
            Driver driver1 = driverService.update(driver);
            if(driver1!=null){
                if(driver1.getCarid()!=null){
                    return new Result(Code.SAVE_OK,driver1,"完善信息成功");
                }else {
                    return new Result(Code.UPDATE_ERR,"密码不能为空");
                }

            }else{
                return new Result(Code.SAVE_ERR,"完善信息失败");
            }


    }
    @PostMapping ("/updatejifen")
    public Result updatejifen(@RequestBody Driver driver){
        int updatejifen = driverService.updatejifen(driver);
        if(updatejifen>0){
            return new Result(Code.UPDATE_OK,updatejifen,"修改成功");
        }else {
            return new Result(Code.UPDATE_ERR,"修改失败");
        }
    }









    //分页查询车辆认证
//    @RequestMapping("/findpage")
//    public Result findpage(@RequestBody page2 Driver){
//        Page<Map> byParams = driverService.findByParams(Driver);
//        if (byParams!=null){
//            return new Result(Code.GET_OK, byParams, "查询成功");
//        } else {
//            return new Result(Code.GET_ERR, "查询失败");
//        }
//    }
}
